<template>
	<div id = 'article-url'>
		<h3 style="padding:0 0 15px 15px">{{ isEdit ? "编辑链接文章" : "添加链接文章"}}</h3>
		<el-form ref="form" :model="form" label-width="100px">
			<el-form-item 
			label="文章标题"
			prop="title"
			:rules="[{ required: true, message: '请输入文章标题', trigger: 'blur' }]">
				<el-input v-model="form.title"></el-input>
			</el-form-item>
			<el-form-item 
			label="文章链接"
			prop="url"
			:rules="[{ required: true, message: '请输入文章链接', trigger: 'blur' }]">
				<el-input v-model='form.url'></el-input>
			</el-form-item>
			<el-form-item
			label="文章类别"
			prop="category_id"
			:rules="[{ required:true, message:'请选择输入文章类别'}]">
				<wt-add-select v-model="form.category_id"  :form = "form" :field = "field"></wt-add-select>
			</el-form-item>
			<el-form-item
			label="文章封面图"
			prop="content.thumb"
			:rules="[{ required: true, message: '请选择文章封面缩略图',trigger: 'blur'}]">
				<wt-image-selector
					:value="form.content.thumb"
					@input="value => { form.content.thumb = value[0] }"
					:parent-id="form.id"
					:multiple="false"
					:action = "action"
				></wt-image-selector>
					<span style="color:#20a0ff;font-size:14px">请选择宽高比为4:3的图片,宽度大于85px效果最佳</span>
			</el-form-item>
			<el-form-item
			label="作者"
			prop="content.author">
				<el-input v-model="form.content.author"></el-input>
			</el-form-item>
			<el-form-item 
			label="发布时间"
			prop="content.publish_at"
			:rules="[{ required: true, message: '请选择发布时间', trigger: 'blur'}]">
				<el-input v-model="form.content.publish_at"></el-input>
			</el-form-item>
			<el-form-item
			label="是否推送"
			prop="is_push"
			v-if="!isEdit">
				<el-radio v-model="form.is_push" label="1">推送</el-radio>
				<el-radio v-model="form.is_push" label="0">不推送</el-radio>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="onSave" :loading="saving" icon="check">保存</el-button>
				<el-button @click="onCancel" icon="close">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
	import request from 'winged/utils/axios'
	import ModelUtils from 'winged/utils/wt-model-utils'
	import ModelService from 'winged/utils/wt-model-service'

	export default {
		name : "new-article-url",
		components:{
		},
		props : {
			model: {
				type: Object,
				required: true,
			},
			value: {
				type: Object,
				default: {}
			},
			paramsData: {
				type: Object,
				default: {}
			},			
			id: {
				type: [Object, String, Number],
				default: null
			},
			type: {
				type: [String,Number],
				default: ''
			}
		},
		data(){
			return{
				isEdit : false,
				form : this.value != null ? ModelUtils.cloneObject(this.value) : ModelUtils.createDefaultObject(this.model),
				action: 'article/upload',
				field : {
					tableName : "category_id",
					returnAllData : true,
					addReturnAllData: false,
					query:{
					method: 'GET',
					addMethod: 'post',
					url: 'common/article/category',
					transfer: false,
					allowAdd: true,
					param:{},
					addParam: ['name'],
					addurl: 'common/article/add_category',
					isSearch: true
				}
				},
				ispush : false,
				saving:false,
				new: {
					method: 'POST',
					url: 'common/article',
					transfer: false,
					extraParams: [
					]
				},
				update: {
					method: 'PUT',
					url: 'common/article',
					transfer: false,
				},
			}
		},
		methods:{
			content(){
				let cont = true;
				if(typeof(this.form.content)=="string"){
					this.form.content = JSON.parse(this.form.content)
				}
				if(this.form.template==1){
						for(let i = 0; i<this.form.content.body.length;i++){
							if(!this.form.content.body[i].title||!this.form.content.body[i].desc||!this.form.content.body[i].url){
								cont = false;
								break;
							}
						}
				}else if(this.form.template==2){
					for(let i = 0; i<this.form.content.body.length;i++){
						if(!this.form.content.body[i].title||!this.form.content.body[i].url){
							cont = false;
							break;
						}
					}
				}
				return cont;
			},
			onSave(){
				this.dosave()
			},
			onCancel(){
				this.$emit('cancel');
			},
			dosave(){
				this.$refs.form.validate((valid) => {
					if (valid&&this.content()) {
						this.saving = true;
						let config;
						if (this.isEdit) {
							config = this.update;
						} else {
							config = this.new;
						}
						if(typeof(this.form.content)=="string"){
							this.form.content = JSON.parse(this.form.content)
						}		
					// ModelUtils.buildQueryExtraParams(config, this.paramsData, this.form);
					request(config.url,config.method,this.form,config.transfer, (data) => {
							this.$emit('saved', data);
							this.saving = false;
							this.$message.success('保存成功');
						}, (error) => {
							this.$message.error('保存失败');
							this.saving = false;
						})
					}
				})
			},
		},
		created(){
			if(this.id){
				this.isEdit = true;
			}else{
				this.isEdit = false
			}
			this.form.type = this.type
			if(!this.form.content){
				this.form.content = {
					publish_at:"",
					thumb:""
				}
			}
		}
	}
</script>